/**
 * <p>Title: ServiceZMCM60.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午08:01:30  余虎  Create
 *</pre>
 */
package com.baosight.sczx.zm.cm.service;

import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.Utils;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMTeammaterialplan;
import com.baosight.sczx.zm.cm.domain.ZMCMTeammaterialplanDetail;

/**采购计划--选择需求计划
 * <p>Title: ServiceZMCMD3.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-8 下午08:01:30  余虎  Create
 *</pre>
 */
public class ServiceZMCMD3 extends ServiceEPBase {

	/**初始化数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD3</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:03:31 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.getBlock(EiConstant.resultBlock).addMeta(new EiColumn("fdEditor"));
		outInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdEditor", UserSession.getUserId());
		outInfo.getBlock(EiConstant.resultBlock).addMeta(new EiColumn("fdChief"));
		outInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdChief",UserSession.getInSessionProperty("orgCode"));
		return outInfo;
	}


	/**查询已审核的需求年计划
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD3</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:03:31 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdParentId = inInfo.getString("fdParentId");//树节点ID
		StaticTools.setQueryParameters(inInfo, "fdYear", fdParentId, true);//添加查询条件
		//StaticTools.setQueryParameters(inInfo, "fdApplicationDeparment",UserSession.getInSessionProperty("orgCode").toString(),true);//当前部门
		
		EiInfo outInfo = new EiInfo();
		outInfo.addBlock(Utils.generateOutBlock(EiConstant.resultBlock, new ZMCMTeammaterialplan(), inInfo, "ZMCMTeammaterialplan.queryInterfaceForYear", false));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));
		return outInfo;
	}

	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD3</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:03:31 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		String fdPlanId = inInfo.getString("fdParentId");
		inInfo.addBlock("result");
		String year = (fdPlanId).substring(0,4);//树节点ID
		String fdEditDate = StaticTools.getDate();
		String loginId = UserSession.getUserId().toString();
		String depId = UserSession.getInSessionProperty("orgCode").toString();
		
		EiInfo insertInfo = new EiInfo();
		insertInfo.addBlock(EiConstant.resultBlock);
		String fdKeyId = TablePk.getInstance().getNextValue("T_CM_STOCK_PLAN");
		insertInfo.getBlock("result").setCell(0, "fdKeyId",fdKeyId);//主键
		insertInfo.getBlock("result").setCell(0, "fdEditor",loginId);
		insertInfo.getBlock("result").setCell(0, "fdChief",depId);
		insertInfo.getBlock("result").setCell(0, "fdYear",year);
		insertInfo.getBlock("result").setCell(0, "fdPlanStatus","草拟");
		insertInfo.getBlock("result").setCell(0, "fdStatus","0");
		insertInfo.getBlock("result").setCell(0, "fdEditDate",fdEditDate);
		
		StringBuffer KeyIds = new StringBuffer();
		String KeyId = "";
		for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
		{
			KeyIds.append(inInfo.getBlock("result").getCellStr(i, "fdKeyId")+";");//获取勾选记录的主键
			KeyId += "'"+inInfo.getBlock("result").getCellStr(i, "fdKeyId")+"'";
			if(i!= inInfo.getBlock("result").getRowCount()-1)
			{
				KeyId += ",";
			}
		}
		
		EiInfo outInfo = super.insert(insertInfo, "ZMCMStockPlan.insert");
//		outInfo = super.update(inInfo,"ZMCMStockPlan.updateForMaterialNumUp");
//		outInfo = super.update(inInfo,"ZMCMStockPlan.updateForPlanSumUp");

	    inInfo.set("fdPlanId", KeyIds.toString());
	    if(inInfo.getBlock("inqu_status")==null)
	    {
	    	inInfo.addBlock("inqu_status");
	    }
	    if(!KeyId.equals(""))
	    {
	    	inInfo.getBlock("inqu_status").setCell(0, "fdPlanId", KeyId);
	    }
	    
	    outInfo.addBlock(Utils.generateOutBlock("Planresult", new ZMCMTeammaterialplanDetail(), inInfo, "ZMCMTeammaterialplanDetail.queryInterfaceCombine", false));
	    outInfo.set("fdStockPlanId", fdKeyId);
	    outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));
		return outInfo;
	}
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD3</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:03:31 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		// TODO Auto-generated method stub
		return super.update(inInfo);
	}

	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMD3</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-8下午08:03:31 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		return inInfo;
	}

}
